<?php
namespace app\admin\controller;
use app\admin\common\Base;
use think\Db;
class Menu extends Base
{
	//菜单列表
    public function index()
    {
    	
    	$data=$this->request->param();
        if(isset($data['ajax'])&&$data['ajax']==1){
            $map='';
            $page=isset($data['page'])?$data['page']:1;
            $page=intval($page);
            $limit=isset($data['rows'])?$data['rows']:10;
            $limit=intval($limit);
            $start=$limit*($page-1);
          
            if($map==''){
                $list =Db::name('menu')->where('pid',0)->order('sort desc,id desc')->limit($start,$limit)->select();
                $count=Db::name('menu')->where('pid',0)->order('sort desc,id desc')->count();
            }else{
                $list =Db::name('menu')->where('pid',0)->order('sort desc,id desc')->limit($start,$limit)->select();  
                $count=Db::name('menu')->where('pid',0)->order('sort desc,id desc')->count();
            }
            foreach ($list as $key => &$val) {
		            $val['text']=$val['node_name'];
		            $val['isCatalog']=$val['status'];//启用状态
		            $val['isExpand']=0;
		            $val['url']=$val['rule'];
		            $val['CreateDate']=date('Y-m-d',$val['create_time']);
		            $children=Db::name('menu')->where('status',1)->where('pid',$val['id'])->order('sort desc,id desc')->select();
		            if(empty($children)){
		                 $val['children']=[];
		            }else{
		                foreach ($children as $k => &$v) {
		                    $v['text']=$v['node_name'];
		                    $v['isCatalog']=$v['status'];//启用状态
		                    $v['isExpand']=0;
		                    $v['url']=$v['rule'];
		                    $v['CreateDate']=date('Y-m-d',$v['create_time']);
		                    $v['children']=[];
		                }
		                $val['children']=$children;
		            } 
		    }

           // $count =count($list);
            return $this->json_data($count,$list); 

        }
    	return $this->fetch();
    }

    //编辑栏目1-1
    public function edit($id){
    		if(empty($id)){
    			$info=[];    		
    		}else{
    			$info=Db::name('menu')->where('id',$id)->find();
    		}
           
           	$cate = self::getCate(); 
           	//halt($cate);          
            $this->assign('cate',$cate);
            $this->assign('info',$info);
            return $this->fetch('edit');
    }
    //编辑栏目1-2
    public function save()
    {
         $status=0;
         $msg="操作失败";
         $data = $this->request->param();
        
         if(empty($data['id'])){
             $update=[
	             'node_name' => $data['node_name'],
	             'pid' => $data['pid'],
	             'rule' => $data['rule'],
	             'sort' => $data['sort'],
	             'status' => 1,
	             'create_time'=>time(),
	             'is_menu'=>2,
	             'update_time'=>time(),
	          ];
	          //执行操作
	         $res=Db::table('menu')->insert($update);
	         if ($res) {
                    adminlog('添加菜单','添加');
	                $status=1;
	                $msg="更新成功";
	          }
         }else{
         	 $id=$data['id'];
         	 if(empty($data['status'])){
	             $data['status']=0;
	         }
	         $update=[
	             'node_name' => $data['node_name'],
	             'pid' => $data['pid'],
	             'rule' => $data['rule'],
	             'sort' => $data['sort'],
	             'status' => $data['status'],
	             'update_time'=>time(),
	          ];
	         //执行更新操作
	         $res=Db::table('menu')->where('id', $id)->update($update);      
	         if ($res) {
                    adminlog('修改菜单','修改');
	                $status=1;
	                $msg="更新成功";
	          }
         }
         return ['status'=>$status, 'msg'=> $msg];
    }

       //删除栏目1-1
    public function del($id){		
    	$status=0;
        $msg="操作失败";
        if(empty($id)){
    		$info=[];    		
    	}else{
    		$info=Db::name('menu')->where('id',$id)->find();
    		$flag=Db::name('menu')->where('pid',$info['id'])->count();
    		if($flag>0){
    			$msg="操作失败,请先删除子菜单";
    		}else{
    			$del=Db::name('menu')->where('id',$id)->delete();
    			if($del){
                    adminlog('删除菜单','删除');
    				$status=1;
       				$msg="删除成功";
    			}
    		}
    	}      
        return ['status'=>$status, 'msg'=> $msg];
          
    }
}
